<?php
require_once(dirname(__FILE__) . '/app.php');

if(!$INI['db']['host']) redirect( WEB_ROOT . '/install.php' );
if($city&&option_yes('rewritecity')){
	//根据用户IP判断用户所在的城市
	$cookie_city = cookieget('city');
	if(empty($cookie_city)){
		$ip_location = new IpLocation();
		$ip = $ip_location->get_client_ip();
		$city_vars = $ip_location->getlocation($ip);
		$city_name_var = iconv("GBK", "UTF-8", $city_vars['country']);
		$cities_var = DB::LimitQuery('category', array(
			'condition' => array( 'zone' => 'city') ,
		));
		
		foreach ($cities_var as $c){
			if(strpos($city_name_var,$c['name'])!==false){
				$city = $c;
				break;
			}else{
				$city = Table::Fetch('category', 'china', 'ename');
			}
		}
	}
	redirect(WEB_ROOT."/{$city['ename']}");
}

$request_uri = 'index';

$sub_id = abs(intval($_GET['sid']));
$area_id = abs(intval($_GET['aid']));

if (option_yes('indexmulti')&& option_yes('indexpage')) {
	$city_id = abs(intval($city['id']));
	$now = time();
	$size = abs(intval($INI['system']['indexteam']));
	if ($size<=1) return current_team($city_id);
	$condition = array( 
			'team_type' => 'normal',
			"begin_time < '{$now}'",
			"end_time > '{$now}'",
			'audit_status' => 0,
			);
	if($sub_id) $condition['sub_id']=$sub_id;
	if($area_id) $condition['area_id']=$area_id;
	$condition[] = "(city_ids like '%@{$city_id}@%' or city_ids like '%@0@%') or (city_ids = '' and city_id in(0,{$city_id}))";
	$count = Table::Count('team', $condition);
	list($pagesize, $offset, $pagestring) = pagestring($count, $size);
	$teams = DB::LimitQuery('team', array(
				'condition' => $condition,
				'order' => 'ORDER BY `sort_order` DESC, `id` DESC',
				'size' => $pagesize,
				'offset' => $offset,
				));
	$disable_multi = true;
	die(require_once( dirname(__FILE__) . '/multi.php'));

}else{
	$team = $teams = index_get_team($city['id'],$sub_id);	
	if ($team && $team['id']) {
		$_GET['id'] = abs(intval($team['id']));
		die(require_once( dirname(__FILE__) . '/team.php'));
	}
	elseif ($teams) {
		$disable_multi = true;
		die(require_once( dirname(__FILE__) . '/multi.php'));
	}
}

include template('subscribe');

